﻿// -----------------------------------------------------------------------
// <copyright file="EncruptOrDecruptForm.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Security_Package.Interface
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Management;
    using Security_Package.Data_Structure;

    /// <summary>
    /// Encrupt Or DecruptForm
    /// </summary>
    public partial class EncruptOrDecruptForm : Form
    {
        /// <summary>
        /// Type Of Operation Make encrypt or decrypt
        /// </summary>
       private Data_Structure.TypeNeed typeOfOperationMake;

       /// <summary>
       /// Algorithm Use
       /// </summary>
       private Data_Structure.AlgorithmUse algorithmUse;

       /// <summary>
       /// Initializes a new instance of the <see cref="EncruptOrDecruptForm"/> class.
       /// </summary>
       /// <param name="operationMake">The operation make.</param>
       /// <param name="algor">The algor.</param>
        public EncruptOrDecruptForm(TypeNeed operationMake, AlgorithmUse algor)
        {
            this.InitializeComponent();
            this.typeOfOperationMake = operationMake;
            this.algorithmUse = algor;
            switch (operationMake)
            {
                case TypeNeed.Decrypt:
                    {
                        this.btnEncryptOrDecrypt.Text = TypeNeed.Decrypt.ToString();
                        this.txtPlanTxt.ReadOnly = true;
                    }

                    break;
                case TypeNeed.Encrypt:
                    {
                        this.btnEncryptOrDecrypt.Text = TypeNeed.Encrypt.ToString();
                        this.txtCipherTxt.ReadOnly = true;
                    }

                    break;
            }

            if (algor == AlgorithmUse.Vignere)
            {
                this.KeyTypeGro.Visible = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the EncryptOrDecrypt control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void EncryptOrDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.algorithmUse != AlgorithmUse.Vignere)
                {
                    switch (this.typeOfOperationMake)
                    {
                        case TypeNeed.Decrypt:
                            {
                                this.txtPlanTxt.Text = Secure.Instance.ScureInput(this.txtCipherTxt.Text, this.typeOfOperationMake, this.algorithmUse, this.txtKeyOrKerWord.Text, Vignere.KeyType.Null);
                            }

                            break;
                        case TypeNeed.Encrypt:
                            {
                                this.txtCipherTxt.Text = Secure.Instance.ScureInput(this.txtPlanTxt.Text, this.typeOfOperationMake, this.algorithmUse, this.txtKeyOrKerWord.Text, Vignere.KeyType.Null);
                            }

                            break;
                    }
                }
                else
                {
                    if (this.radioAuto.Checked)
                    {
                        switch (this.typeOfOperationMake)
                        {
                            case TypeNeed.Decrypt:
                                {
                                    this.txtPlanTxt.Text = Secure.Instance.ScureInput(this.txtCipherTxt.Text, this.typeOfOperationMake, this.algorithmUse, this.txtKeyOrKerWord.Text, Vignere.KeyType.Auto);
                                }

                                break;
                            case TypeNeed.Encrypt:
                                {
                                    this.txtCipherTxt.Text = Secure.Instance.ScureInput(this.txtPlanTxt.Text, this.typeOfOperationMake, this.algorithmUse, this.txtKeyOrKerWord.Text, Vignere.KeyType.Auto);
                                }

                                break;
                        }
                    }
                    else
                    {
                        switch (this.typeOfOperationMake)
                        {
                            case TypeNeed.Decrypt:
                                {
                                    this.txtPlanTxt.Text = Secure.Instance.ScureInput(this.txtCipherTxt.Text, this.typeOfOperationMake, this.algorithmUse, this.txtKeyOrKerWord.Text, Vignere.KeyType.Repeat);
                                }

                                break;
                            case TypeNeed.Encrypt:
                                {
                                    this.txtCipherTxt.Text = Secure.Instance.ScureInput(this.txtPlanTxt.Text, this.typeOfOperationMake, this.algorithmUse, this.txtKeyOrKerWord.Text, Vignere.KeyType.Repeat);
                                }

                                break;
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("the operation fail sure from your data and try again");
            }
        }
    }
}
